<template>
  <view class="normal-login-container">
    <view class="tenantList">
      <text class="title">请选择要登录的租户：</text>
      <view class="tenant-box">
        <view v-for="(item, index) in tenantList" class="tenant-item" @click="handleItem(item)">
          <text class="cph">{{item.enterName}}</text>
        </view>
      </view>
    </view>
  </view>
</template>

<script>
  import func from '@/utils/funlogin.js';
  import { getDriving } from '@/api/me.js';
  import { getPhone, getTenentList, wxPhoneLogin, getTenantsByOpenId, getPhoneByOpenid } from '@/api/login.js';
  import { setTenantId, setDriverId, getTenantId, setToken, removeTenantId } from '@/utils/auth.js';
  import { toast } from '../../utils/common';

  export default {
    name: 'Login',
    data() {
      return {
        tenantList: [],
        openid: ''
      };
    },
    onLoad(options) {

      this.openid = options.openid
      this.getTenants()
    },
    methods: {
      async getTenants() {
        let res1 = await getTenantsByOpenId(this.openid);
        this.tenantList = res1.data;
      },
      async handleItem(e) {
        console.log(e);
        setTenantId(e.id);
        let number = await getPhoneByOpenid(this.openid);
        console.log(number, '这是租户为多个的时候获取回来的手机号================')
        this.login(number.data);
      },
      async login(number) {
        let code = await func.wxLogin();
        console.log(code, '这是登陆接口之前获取的code================')
        let data = {
          phoneCode: number,
          loginCode: code
        };
        wxPhoneLogin(data).then((res) => {
          if (res.code == 0) {
            setToken({
              'accessToken': res.data.accessToken,
              'refreshToken': res.data.refreshToken,
            })
            setDriverId(res.data.userId)
            this.getUserInfor()
          } else {
          }
        });
      },
      //获取司机用户信息
      async getUserInfor() {
        try {
          let res = await getDriving();
          if (res.code == 0) {
            console.log('登录后判断用户有没有实名: ', res);
            if (!!res.data) {
              if (res.data.isIdIdentified) {
                console.log('实名了: ');
                uni.setStorageSync('sjxxData', res.data);
                if (uni.getStorageSync('ysjhData')) {
                  console.log('进入接单页面: ');
                  this.$tab.reLaunch('/pages/index');
                } else {
                  console.log('进入首页: ');
                  this.$tab.reLaunch('/pages/goodsSource');
                }
              } else {
                // 身份实名没有认证
                console.log('登录后没有实名: ');
                if (uni.getStorageSync('ysjhData')) {
                  console.log('进入实名制页面code: ');
                  // uni.navigateTo({
                  // });
                } else {
                  console.log('进入实名制页面无code: ');
                }
              }
            } else {
              this.$tab.reLaunch('/pages/login');
            }
          }
        } catch (e) {
          return false;
        }
      },
    }
  };
</script>

<style lang="scss">
  page {
    background-color: #ffffff;
  }

  .tenantList {
    z-index: 99999;
    background: #fff;
    padding: 35rpx;

    .title {
      font-size: 35rpx;
      margin-bottom: 36rpx;
      display: block;
    }

    .tenant-item {
      text-align: center;
      height: 68rpx;
      font-size: 32rpx;
      line-height: 68rpx;
      background: #dedede;
      margin-bottom: 3rpx;
      color: #666;
      cursor: pointer;
      padding: 6rpx 0;
    }

    .tenant-item:hover {
      background: #ececec;
    }
  }

  .normal-login-container {
    width: 100%;

    .logo-content {
      width: 100%;
      font-size: 20rpx;
      text-align: center;

      image {
        border-radius: 4rpx;
      }
    }

    .login-form-content {
      text-align: center;

      width: 100%;

      .input-item {
        margin: 20px auto;
        background-color: #f5f6f7;
        height: 45px;
        border-radius: 20px;

        .icon {
          font-size: 38rpx;
          margin-left: 10px;
          color: #999;
        }

        .input {
          width: 100%;
          font-size: 14px;
          line-height: 20px;
          text-align: left;
          padding-left: 15px;
        }
      }

      .login-btn {
        margin-bottom: 40rpx;
        height: 90rpx;
      }
    }

    .read {
      margin-bottom: 40rpx;
      padding: 20rpx;
    }

    .xieyi {
      color: #333;

      padding: 20rpx;
    }
  }

  .login-code-img {
    height: 45px;
  }
</style>